package com.scgcxx.service.safty.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.scgcxx.dto.MenuDto;
import com.scgcxx.mapper.safty.HomeMapper;
import com.scgcxx.model.Module;
import com.scgcxx.service.safty.HomeService;
@Service
public class HomeServiceImpl implements HomeService {
	
	@Autowired
	public HomeMapper homeMapper;
	
	@Override
	public List<MenuDto> getMenus() {		
		List<Module> moduleList = homeMapper.findMenus();
		
		List<MenuDto> mainMenuList = new ArrayList<MenuDto>();
		MenuDto currMainMenu=null;
		for(Module m : moduleList) {
			if(m.getM_pid()==null) {
				currMainMenu=new MenuDto();
				currMainMenu.setMenuId(m.getM_id());
				currMainMenu.setMenuName(m.getM_name());
				currMainMenu.setSubMenuList(new ArrayList<MenuDto>());	
				mainMenuList.add(currMainMenu);
			}else {
				MenuDto subMenu=new MenuDto();
				subMenu.setMenuId(m.getM_id());
				subMenu.setMenuName(m.getM_name());
				subMenu.setMenuUrl(m.getM_url());
				
				currMainMenu.getSubMenuList().add(subMenu);
			}
		}
		
		return mainMenuList;
	}

}
